package com.lancoo.ccas53.entity;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.lancoo.ccas53.pojo.vo.TeacherGroup;
import com.lancoo.ccas53.pojo.vo.TeacherGroupTree;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * <p>
 * 教师
 * </p>
 *
 * @author quin.song
 * @since 2024-05-20
 */
@Data
@Accessors(chain = true)
@EqualsAndHashCode(of = {"taskId", "baseTeacherId"})
@ApiModel(value = "Teacher对象", description = "教师")
public class Teacher implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "teacher_id", type = IdType.AUTO)
    private Long teacherId;
    @ApiModelProperty("基础教师id")
    private String baseTeacherId;

    private Integer taskId;
    @ApiModelProperty("教师名称")
    private String teacherName;
    @ApiModelProperty("教师头像")
    private String imageUrl;
    @ApiModelProperty("基础平台学院id")
    private String baseCollegeId;
    @ApiModelProperty("基础平台学院名称")
    private String collegeName;
    @ApiModelProperty("所属校区")
    private Integer campusId;
    @ApiModelProperty("所属校区名称")
    private String campusName;
    private String groupId;
    private String groupName;

    @ApiModelProperty("男、女。保密")
    private String sex;

    @ApiModelProperty("教师职称")
    private String professionalTitle;

    @ApiModelProperty("1、任课教师 2、助教教师")
    private Integer identity;

    @ApiModelProperty("是否允许跨校区排课 0未开启  1已开启")
    private Integer isCampusCross;

    @ApiModelProperty("是否设置规则 1：已设置，2未设置")
    private Integer ruleSet;
    private String schoolId;
    private String term;

    @TableField(exist = false)
    @ApiModelProperty("教学班id,获取教学班详情时使用")
    private Long teachingClassId;

    @TableField(exist = false)
    @ApiModelProperty("对应授课类型 0:环节  1讲授 2实验 3上机 4其他")
    private Integer hourType;

    @TableField(exist = false)
    private Boolean scheduleState = true;

    public static List<TeacherGroupTree> toTree(List<Teacher> teacherList) {
        List<TeacherGroupTree> trees = new ArrayList<>();
        teacherList.forEach(o -> {
            if (StrUtil.isEmpty(o.getBaseCollegeId())) {
                o.setBaseCollegeId("000");
                o.setCollegeName("未知学院");
            }
        });

        for (Teacher teacher : teacherList) {
            String baseCollegeId = teacher.getBaseCollegeId();
            String collegeName = teacher.getCollegeName();

            TeacherGroupTree groupTree = null;
            for (TeacherGroupTree c : trees) {
                if (c.getBaseCollegeId().equals(baseCollegeId)) {
                    groupTree = c;
                    break;
                }
            }
            if (groupTree == null) {
                groupTree = new TeacherGroupTree();
                groupTree.setBaseCollegeId(baseCollegeId);
                groupTree.setCollegeName(collegeName);
                groupTree.setTeacherGroupList(new ArrayList<TeacherGroup>());
                trees.add(groupTree);
            }

            String groupId = teacher.getGroupId();
            String groupName = teacher.getGroupName();
            TeacherGroup teacherGroup = null;
            for (TeacherGroup b : groupTree.getTeacherGroupList()) {
                if (b.getGroupId().equals(groupId)) {
                    teacherGroup = b;
                    break;
                }
            }
            if (teacherGroup == null) {
                teacherGroup = new TeacherGroup();
                teacherGroup.setGroupId(groupId);
                teacherGroup.setGroupName(groupName);
                teacherGroup.setTeacherList(new ArrayList<Teacher>());
                groupTree.getTeacherGroupList().add(teacherGroup);
            }

            teacherGroup.getTeacherList().add(teacher);
            teacherGroup.setSum(teacherGroup.getSum() + 1);
            groupTree.setSum(groupTree.getSum() + 1);

            teacherGroup.setScheduleState(teacher.getScheduleState() ? teacherGroup.getScheduleState() : false);
            groupTree.setScheduleState(teacher.getScheduleState() ? groupTree.getScheduleState() : false);
        }
        trees.sort(Comparator.comparing(TeacherGroupTree::getCollegeName));
        return trees;
    }
}
